
//
//  LYClassInfoViewController.m
//  音伴尔家听宝
//
//  Created by Burgess on 2017/6/16.
//  Copyright © 2017年 Burgess. All rights reserved.
//

#import "LYClassInfoViewController.h"
#import "LYMusicBtnWindowSingleton.h"
#import "LYRadioTableViewCell.h"
#import "LYClassInfoModel.h"
#import "LYHeadViewModel.h"
#import "LYTableHeadView.h"

#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImage+AFNetworking.h>
#import <UIImageView+AFNetworking.h>

@interface LYClassInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *classInfoModel;
@property (nonatomic, strong) LYHeadViewModel *headViewModel;

@end

@implementation LYClassInfoViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource  = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    
    self.navigationItem.title = self.title;
    
    LYMusicBtnWindowSingleton *musicwindow = [LYMusicBtnWindowSingleton sharedMusicBtnWindowSingleton:self];
    musicwindow.windowLevel = UIWindowLevelAlert+300;
    musicwindow.frame = CGRectMake((SCREEN_WIDTH-75)/2, (SCREEN_HEIGHT-75), 75, 75);
    musicwindow.hidden = NO;
    [musicwindow makeKeyAndVisible];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LYRadioTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"secondcell"];
    
    [self getClassListData];
}

-(void)getClassListData{
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:config];
    NSDictionary *paramDic = @{@"courseid":self.courseId};
    
    [manager GET:@"http://cute.fcloudclass.com/api.php/Audio/audio_by_courseid" parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
         self.classInfoModel = [LYClassInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"audio_list"]];
        NSDictionary *dic =responseObject[@"data"][@"course"];
        self.headViewModel  = [LYHeadViewModel appWithDict:dic];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
                   });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.classInfoModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LYRadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondcell" forIndexPath:indexPath];
    
    if (cell == NULL) {
        cell =  [[LYRadioTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"secondcell"];
    }
    
    cell.indicateView.hidden = YES;
    cell.classInfoModel = self.classInfoModel[indexPath.row];
    cell.numLabel.text  = [NSString stringWithFormat:@"%ld",(indexPath.row+1 )];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LYTableHeadView * headView =  [[[NSBundle mainBundle] loadNibNamed:@"LYTableHeadView" owner:NULL options:NULL]firstObject ];
    
    [headView.courseNameImg setImageWithURL:[NSURL URLWithString:self
                                             .headViewModel.mob_small_img]];
    headView.introduceLabel.text = self.headViewModel._description;
    [headView.littleIconimg setImageWithURL:[NSURL URLWithString:self.headViewModel.mob_icon_120]];
    headView.catLabel.text = self.headViewModel.cat_name;
    
    return headView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 240;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LYRadioTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.indicateView.hidden = NO;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    LYRadioTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.indicateView.hidden = YES;
}

#pragma mark -<UIScrollViewDelegate>

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y  >80) {
        [self.navigationController.navigationBar setBackgroundImage:NULL forBarMetrics:UIBarMetricsDefault];
    }else{
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    }
}



@end
